<?php
	$fh = fopen("log.txt", "a");
?>
<?php
    
	class MyForm
	{
		/**
		 * Expected $_POST structure
		 * $_POST = array(
		 * 		'dob' => 1990-09-16,
		 * );
		 */
		private $post_structure = array(
		   /* 'applicant_type',
		    'title',
		    'surname',
		    'givenname',
		    'gender',
		    'dob',
		    'fax_number_ext',
		    'fax_number',
		    'home_phone_number_ext',
		    'home_phone_number',
		    'work_phone_number_ext',
		    'work_phone_number',
		    'mobile_phone_number_ext',
		    'mobile_phone_number',
		    'email_address',
			'martial_status',
			'residential_status',
			'nationality',
			'resident_of_singapore',
			'resident_of',
			'current_residential_address',
			'current_residential_address_postcode',
			'current_residential_year',
			'current_residential_month',
			'postal_address',
			'postal_address_postcode',
			'after_loan_postal_address',
			'after_loan_postal_address_postcode',
			'previous_residential_address',
			'previous_residential_address_postcode',
			'previous_residential_year',
			'previous_residential_month',
			'licence_number',
			'licence_state',
			'licence_expiry_date',
			'employment_status',
			'gross_salary_applicant_1',
			'employment_level',*/
			
			
		);
        
		private $post;

		public function __construct($post)
		{
			$status = true;

			foreach($this->post_structure as $key)
			{
				// check all required $_POST data exist.
				if(!array_key_exists($key, $post))
				{
					$status = false;
					break;
				}
			}

			if($status)
			{
				// Validation passed
				$this->post = $post;
			}
			else
			{
				// Validation failed
				throw new Exception('Form: Required data missing!');
			}
		}

		public function getAge()
		{
			// Example $this->post['dob'] => 1990-06-19
			list($year, $month, $day) = explode("-", $this->post['dob']);

			$year_diff  = date("Y") - $year;
			$month_diff = date("m") - $month;
			$day_diff   = date("d") - $day;

			if ($day_diff < 0 || $month_diff < 0)
			{
			  $year_diff--;
			}

			return $year_diff;
		}

		public function getAllKeyValuePairs()
		{
			return $this->post;
		}
	}


?>
<?php
	fclose($fh);
?>